<template>
  <div>
    <!-- 面包屑导航区域 -->
    <el-breadcrumb separator="/">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品列表</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片区域 -->
    <el-card>
      <!-- 检索行 添加商品 -->
      <el-row :gutter="20">
        <el-col :span="8">
          <el-input
            placeholder="请输入内容"
            clearable
            @clear="onSearchInputClear"
            v-model="queryObj.query"
          >
            <el-button @click="onSearch" slot="append" icon="el-icon-search"></el-button>
          </el-input>
        </el-col>
        <el-col :span="4">
          <el-button type="primary" @click="goToAdd">添加商品</el-button>
        </el-col>
      </el-row>
      <!-- 表格区域 -->
      <el-table :data="goodsList" border stripe>
        <el-table-column type="index" label="#"></el-table-column>
        <el-table-column label="商品名称" prop="goods_name"></el-table-column>
        <el-table-column label="商品价格(元)" prop="goods_price" width="91"></el-table-column>
        <el-table-column label="商品重量" prop="goods_weight" width="70"></el-table-column>
        <el-table-column label="创建时间" width="140">
          <template v-slot="scope">{{dateFormater(scope.row.add_time)}}</template>
        </el-table-column>
        <el-table-column label="操作" width="120">
          <template v-slot="scope">
            <el-button size="mini" type="primary" icon="el-icon-edit"></el-button>
            <!-- 删除按钮 -->
            <el-button
              @click="removeGoods(scope.row.goods_id)"
              size="mini"
              type="danger"
              icon="el-icon-delete"
            ></el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页区域 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryObj.pagenum"
        :page-sizes="[5, 10, 15, 20]"
        :page-size="queryObj.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      ></el-pagination>
    </el-card>
  </div>
</template>
<script>
export default {
  data() {
    return {
      // 查询参数
      queryObj: {
        query: "",
        pagenum: 1,
        pagesize: 10
      },
      //   商品列表数据
      goodsList: [],
      total: 0
    };
  },
  created() {
    this.getGoodsList();
  },
  methods: {
    //   获取商品列表数据
    async getGoodsList() {
      const { data: res } = await this.$http.get("goods", {
        params: this.queryObj
      });
      if (res.meta.status !== 200)
        return this.$message.error("获取商品列表失败!");
      this.goodsList = res.data.goods;
      this.total = res.data.total;
    },
    // 格式化事件的方法
    dateFormater(dtStr) {
      const dt = new Date(dtStr);
      const y = dt.getFullYear();
      const m = this.padZero(dt.getMonth() + 1);
      const d = this.padZero(dt.getDate());
      const hh = this.padZero(dt.getHours());
      const mm = this.padZero(dt.getMinutes());
      const ss = this.padZero(dt.getSeconds());

      return `${y}-${m}-${d} ${hh}:${mm}:${ss}`;
    },
    // 补零函数
    padZero(n) {
      return n > 9 ? n : "0" + n;
    },
    // 页面条数发生变化
    handleSizeChange(newSize) {
      this.queryObj.pagesize = newSize;
      this.getGoodsList();
    },
    // 当前页码值发生变化
    handleCurrentChange(newPage) {
      this.queryObj.pagenum = newPage;
      this.getGoodsList();
    },
    // 搜索区域
    onSearch() {
      this.queryObj.pagenum = 1;
      this.getGoodsList();
    },
    // 清空搜索框
    onSearchInputClear() {
      this.queryObj.pagenum = 1;
      this.getGoodsList();
    },
    // 删除
    async removeGoods(goods_id) {
      const confirmResult = await this.$confirm(
        "此操作将永久删除该商品, 是否继续?",
        "提示",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }
      ).catch(err => err);
      if (confirmResult !== "confirm") return;
      const { data: res } = await this.$http.delete(`goods/${goods_id}`);
      if (res.meta.status !== 200) return this.$message.error("删除商品失败！");

      this.$message.success("删除商品成功！");
      this.getGoodsList();
    },
    // 点击按钮,跳转到添加商品的组件页面
    goToAdd() {
      this.$router.push("goods/add");
    }
  }
};
</script>
<style lang="less" scoped>
</style>
